﻿using Common;
using DadayunApi.Model;
using KhtApi.BLL;
using KhtApi.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace KhtMaintain.Model
{
    /// <summary>
    /// 产品订单
    /// </summary>
    internal class ProductOrder
    {
        
        public string SoNo { get; set; }

        public DateTime SoDate { get; set; }
        public string ItemCode { get; set; }
       
        public string ItemName { get; set; }
        public string ModelNo { get; set; }
        public string Spec { get; set; }
        public string Version { get; set; }
        public string Unit { get; set; }
        public double TotalQty { get; set; }

        public EOrigin Origin { get; set; }
        /// <summary>
        /// 自产|外购|客供|委外
        /// </summary>
        public string OriginCn
        {
            get
            {
                return Origin.ToString();
            }
            set
            {
                if (Enum.TryParse(value, out EOrigin origin))
                {
                    Origin = origin;
                }
                else
                {
                    throw (new Exception("不支持的产品来源类型！请确保输入的字符串为‘自产|外购|客供|委外’四种之一"));
                }
            }
        }
        public string Saler { get; set; }

        public string CusName { set; get; }
        public string CusNo { get; set; }
        public string CusPoNo { get; set; }
        public string Catagory1 { get; set; }
        public string Catagory2 { get; set; }
        public string Catagory3 { get; set; }

        public PmcProduct ToPmcProduct()
        {
            PmcProduct product = new PmcProduct
            {
                Catagory1 = Catagory1,
                Catagory2 = Catagory2,
                Catagory3 = Catagory3,
                ItemCode = ItemCode,
                ItemName = ItemName,
                ModelNo = ModelNo,
                Origin = Origin,
                SoNo = SoNo,
                Spec = Spec,
                TotalQty = TotalQty,
                Unit = Unit,
                Version = Version
            };
            return product;
        }
        /// <summary>
        /// 从销售订单进度看板调入产品订单清单
        /// </summary>
        /// <param name="billboard">看板</param>
        /// <returns></returns>
        public static List<ProductOrder> FromSoBillboard(IEnumerable< SoBillBoard> billboard, ReportBLL reportBLL)
        {
            List<ProductOrder> list = new List<ProductOrder>();
            foreach (var s in billboard)
            {
                if (string.IsNullOrEmpty(s.ItemCode)) continue;
                if (string.IsNullOrEmpty(s.OriginCn))
                {//旧的订单里没有产品来源字段，需要在这里补上
                    var prdInfo = reportBLL.GetProductInfo(s.ItemCode);
                    s.OriginCn = prdInfo.OriginCn;
                }
                list.Add(new ProductOrder
                {
                    SoNo = s.SoNo,
                    SoDate = s.SoDate,
                    CusName = s.CusName,
                    CusNo = s.CusNo,
                    CusPoNo = s.CusPoNo,
                    Saler = s.Saler,
                    ItemCode = s.ItemCode,
                    ItemName = s.ItemName,
                    ModelNo = s.ModelNo,
                    Spec = s.ItemSpec,
                    Unit = s.Unit,
                    OriginCn = s.OriginCn,
                    TotalQty = s.SoQty,
                    Version = s.Version,
                    Catagory1=s.Catagory1,
                    Catagory2=s.Catagory2,
                    Catagory3=s.Catagory3
                });
            }
            return list;
        }
    }
}
